package org.deng.learn.jvm;

import org.openjdk.jol.info.ClassLayout;

/**
 * @Project
 * @Module
 * @JDK_Vserion 1.8
 * @Author DengLibin
 * @Email dlb@smarthse.cn
 * @Desc
 * @since 2019/9/24 0024 9:56
 */
public class TestJvm {

    public static void main(String[] args){
        String s1 = "abc";
        String s2 = new String("abc");
        String s3 = new String("abc");
        /* javap -v TestJvm.class得到指令如下：
         0: new           #2                  // class java/lang/String        创建一个对象，并将其引用值压入栈顶
         3: dup                                                                复制栈顶数值并将复制值压入栈顶
         4: ldc           #3                  // String abc                    将int, float或String型常量值从常量池中推送至栈顶
         6: invokespecial #4                  // Method java/lang/String."<init>":(Ljava/lang/String;)V   调用超类构造方法，实例初始化方法，私有方法
         9: astore_1                                                            将栈顶引用型数值存入第二个本地变量
        10: getstatic     #5                  // Field java/lang/System.out:Ljava/io/PrintStream; 获取指定类的静态域，并将其值压入栈顶
        13: aload_1                                                                               将第二个引用类型本地变量推送至栈顶
        14: invokestatic  #6                  // Method org/openjdk/jol/info/ClassLayout.parseInstance:(Ljava/lang/Object;)Lorg/openjdk/jol/info/ClassLayout; 调用静态方法
        17: invokevirtual #7                  // Method org/openjdk/jol/info/ClassLayout.toPrintable:()Ljava/lang/String;  调用实例方法
        20: invokevirtual #8                  // Method java/io/PrintStream.println:(Ljava/lang/String;)V  调用实例方法
        23: return
        *
        * */
        System.out.println(s1==s2);
        System.out.println(s2==s3);
        System.out.println(ClassLayout.parseInstance(s1).toPrintable());
        System.out.println(ClassLayout.parseInstance(s2).toPrintable());
        System.out.println(ClassLayout.parseInstance(s3).toPrintable());
    /*s2对象内存数据如下：
     OFFSET  SIZE     TYPE DESCRIPTION                               VALUE
      0     4          (object header)                           01 00 00 00 (00000001 00000000 00000000 00000000) (1)
      4     4          (object header)                           00 00 00 00 (00000000 00000000 00000000 00000000) (0)
      8     4          (object header)                           da 02 00 f8 (11011010 00000010 00000000 11111000) (-134216998)
     12     4   char[] String.value                              [a, b, c]
     16     4      int String.hash                               0
     20     4          (loss due to the next object alignment)
     */
    }
}
